﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace change_names
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private delegate void execFn(string path, string prefix);
        private string path = "";

        public MainWindow()
        {
            InitializeComponent();
        }

        private void SelectPath_Click(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = "All Files (*.*)|*.*"
            };
            var result = openFileDialog.ShowDialog();
            if (result == true)
            {
                var file = openFileDialog.FileName;
                path = file.Substring(0, file.LastIndexOf('\\'));
                path += "\\";
                //path = path.Substring(0, path.LastIndexOf('\\'));
                textBox.Text = path;
                string spath = path.Replace('\\', '_');

                string npath = path.Replace("\\", "");
            }
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            if (path != "")
            {
                execAllFolder(new DirectoryInfo(path));
                Application.Current.Shutdown();
            }
        }

        private void execAllFolder(DirectoryInfo dir)
        {
            string prefix = dir.FullName.Replace(path, "");
            prefix = prefix.Replace('\\', '_');

            if (prefix != "")
            {
                FileInfo[] allFile = dir.GetFiles();
                foreach (FileInfo fi in allFile) fi.MoveTo(dir.FullName.TrimEnd('\\') + "\\" + prefix + fi.Name);
            }

            DirectoryInfo[] allDir = dir.GetDirectories();
            foreach (DirectoryInfo d in allDir) execAllFolder(d);
        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }
    }
}
